package com.tbs.struct.tree.impl.mapper;

import com.tbs.cache.expire.annotation.ExpireOn;
import com.tbs.sql.mapper.IFrameworkMapper;
import com.tbs.struct.tree.impl.entity.TreeNodeEntity;
import org.apache.ibatis.annotations.Select;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author tongj
 */
@Repository
public interface TreeNodeMapper extends IFrameworkMapper<TreeNodeEntity, Long> {

    @Cacheable(cacheNames = "tree_node_entity_cache", key = "#id", unless = "#result == null", condition = "#id!=null")
    @ExpireOn(value = 2, timeUnit = TimeUnit.MINUTES, cacheNames = "tree_node_entity_cache")
    @Select("select * from tree_node where id = #{id}")
    TreeNodeEntity getNodeById(Long id);

    @Select("select * from tree_node where node_id = #{nodeId} and group_name=#{treeGroup}")
    List<TreeNodeEntity> getNodeByNodeIdAndTreeGroup(String nodeId, String treeGroup);

    @Select("select group_name from tree_node where parent_id is null group by group_name")
    List<String> listAllGroup();

    @Select("select * from tree_node where parent_id is null and group_name = #{groupName}")
    List<TreeNodeEntity> getRootNodeByGroupName(String groupName);

    @Select("select * from tree_node where parent_id = #{nodeId} and group_name=#{treeGroup}")
    List<TreeNodeEntity> getChildren(String nodeId, String treeGroup);
}
